package com.skeeey.aim.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class UNZipUtil {
	public static boolean unZip(final File sourceFile, final String targetDir) {
		boolean result = false;
		ZipFile zipfile = null;
		try {
			zipfile = new ZipFile(sourceFile, "GBK");
			@SuppressWarnings("rawtypes")
			Enumeration zipList = zipfile.getEntries();
			
			while (zipList.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) zipList.nextElement();
				String fileName = zipEntry.getName().replaceAll("\\(\\d+\\)", "").replaceAll("（\\d+）", "");
				if (zipEntry.isDirectory()) {
					File dir = new File(targetDir + File.separator + fileName);
					if(!dir.exists()) {
						dir.mkdirs();
					}
					continue;
				}
				int slashPos = fileName.indexOf("\\");
				if(slashPos > 0) {
					int lastSlashPos = fileName.lastIndexOf("\\");
					String pathName = fileName.substring(0, lastSlashPos).replaceAll("\\\\", "/");
					fileName = fileName.substring(lastSlashPos + 1); 
					String subTargetDir = targetDir + File.separator + pathName;
					File subDir = new File(subTargetDir);
					if(!subDir.exists()) {
						subDir.mkdirs();
					}
					result = saveFile(subTargetDir, fileName, zipfile, zipEntry);
				} else {
					int lastBackslashPos = fileName.lastIndexOf("/");
					if(lastBackslashPos > 0) {
						String _fileName = fileName.substring(lastBackslashPos + 1);
						String subDir = fileName.substring(0, lastBackslashPos);
						result = saveFile(targetDir + File.separator + subDir, _fileName, zipfile, zipEntry);
					} else {
						result = saveFile(targetDir, fileName, zipfile, zipEntry);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		} finally {
			try {
				if (zipfile != null) {
					zipfile.close();
				}
			} catch (IOException e) {
				// do nothing
			}
		}
		
		return result;
	}
	
	private static boolean saveFile(String targetDir, String fileName, ZipFile zipfile, ZipEntry zipEntry) {
		boolean result = true;
		OutputStream out = null;
		InputStream in = null;
		byte[] buffer = new byte[20480];
		try {
			//System.out.println("Debug::[file name]>>" + targetDir + File.separator + fileName);
			File dir = new File(targetDir);
			if(!dir.exists()) {
				dir.mkdirs();
			}
			out = new BufferedOutputStream(new FileOutputStream(targetDir + File.separator + fileName));
			in = new BufferedInputStream(zipfile.getInputStream(zipEntry));
			int readLen = 0;
			while ((readLen = in.read(buffer, 0, 1024)) != -1) {
				out.write(buffer, 0, readLen);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				
				if(out != null) {
					out.close();
				}
			} catch (IOException e) {
				// do nothing
			}
		}
		return result;
	}

	public static void main(String[] args) {
		unZip(new File("/mnt/2010301-乌海庞大-2013-10-31.zip"), "/home/skeeey/tmp");
		unZip(new File("/mnt/2010301-乌海庞大-2013-10-31_2.zip"), "/home/skeeey/tmp");
	}

}
